<!DOCTYPE
html >
  <html>
<head>
    <meta charset="UTF-8">
    <title>视频抽帧工具</title>
    <style>
        :root {
            --bg-primary: #121212;
            --bg-secondary: #1e1e1e;
            --bg-tertiary: #2d2d2d;
            --text-primary: #e0e0e0;
            --text-secondary: #a0a0a0;
            --accent-primary: #7c4dff;
            --accent-secondary: #5e35b1;
            --success: #4caf50;
            --success-dark: #388e3c;
            --error: #f44336;
            --border-radius: 8px;
            --transition: all 0.3s ease;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', 'Roboto', sans-serif;
            background-color: var(--bg-primary);
            color: var(--text-primary);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 2rem;
        }
        
        h1 {
            font-size: 2rem;
            margin-bottom: 1.5rem;
            text-align: center;
            color: var(--text-primary);
        }
        
        .container {
            width: 100%;
            max-width: 600px;
            display: flex;
            flex-direction: column;
            gap: 1.5rem;
        }
        
        .card {
            background-color: var(--bg-secondary);
            border-radius: var(--border-radius);
            padding: 1.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
            transition: var(--transition);
        }
        
        #file-select-area {
            border: 2px dashed var(--accent-primary);
            padding: 2rem;
            text-align: center;
            cursor: pointer;
            border-radius: var(--border-radius);
            transition: var(--transition);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 150px;
        }
        
        #file-select-area:hover {
            background-color: rgba(124, 77, 4d, 0.1);
            border-color: var(--accent-secondary);
        }
        
        #file-select-area p {
            color: var(--text-secondary);
            margin-bottom: 0.5rem;
        }
        
        #file-info {
            margin-top: 1rem;
            font-weight: 500;
            color: var(--accent-primary);
            word-break: break-word;
        }
        
        .button {
            padding: 0.75rem 1.5rem;
            border: none;
            border-radius: var(--border-radius);
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: var(--transition);
            color: white;
            width: 100%;
        }
        
        #browse-button {
            background-color: var(--accent-primary);
        }
        
        #browse-button:hover {
            background-color: var(--accent-secondary);
        }
        
        #start-button {
            background-color: var(--success);
            margin-top: 1rem;
        }
        
        #start-button:hover:not(:disabled) {
            background-color: var(--success-dark);
        }
        
        #start-button:disabled {
            background-color: var(--bg-tertiary);
            color: var(--text-secondary);
            cursor: not-allowed;
        }
        
        .controls {
            display: flex;
            flex-direction: column;
            gap: 1rem;
        }
        
        .input-group {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            justify-content: center;
        }
        
        .input-group label {
            color: var(--text-secondary);
        }
        
        .input-group input {
            background-color: var(--bg-tertiary);
            border: 1px solid #444;
            color: var(--text-primary);
            padding: 0.5rem;
            border-radius: 4px;
            width: 70px;
            text-align: center;
        }
        
        #status {
            background-color: var(--bg-tertiary);
            padding: 1rem;
            border-radius: var(--border-radius);
            text-align: center;
            color: var(--text-secondary);
            min-height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .icon {
            margin-right: 0.5rem;
            font-size: 1.2rem;
        }
        
        /* Simple animation for status updates */
        @keyframes pulse {
            0% { opacity: 0.6; }
            50% { opacity: 1; }
            100% { opacity: 0.6; }
        }
        
        .processing {
            animation: pulse 1.5s infinite;
        }
    </style>
</head>
<body>
    <h1>视频抽帧工具</h1>
    
    <div class="container">
        <!-- File selection card -->
        <div class="card">
            <div id="file-select-area">
                <p>点击选择视频文件</p>
                <p id="file-info"></p>
            </div>
            
            <button id="browse-button" class="button">
                <span class="icon">📁</span>选择视频文件
            </button>
        </div>
        
        <!-- Controls card -->
        <div class="card controls">
            <h2>截图设置</h2>
            
            <div class="input-group">
                <label for="minutes">截图间隔:</label>
                <input type="number" id="minutes" name="minutes" min="0" value="0">
                <label for="minutes">分</label>
                <input type="number" id="seconds" name="seconds" min="0" max="59" value="10">
                <label for="seconds">秒</label>
            </div>
            
            <button id="start-button" class="button" disabled>请先选择文件</button>
        </div>
        
        <!-- Status card -->
        <div class="card">
            <div id="status">等待操作...</div>
        </div>
    </div>

    <!-- Hidden file input -->
    <input type="file" id="hidden-file-input" accept="video/*" style="display: none;">

    <script>
        let pythonProvidedFilePath = null; // Store path confirmed by Python
        const fileSelectArea = document.getElementById('file-select-area');
        const browseButton = document.getElementById('browse-button');
        const hiddenFileInput = document.getElementById('hidden-file-input');
        const fileInfo = document.getElementById('file-info');
        const statusDiv = document.getElementById('status');
        const startButton = document.getElementById('start-button');
        const minutesInput = document.getElementById('minutes');
        const secondsInput = document.getElementById('seconds');

        // --- Trigger File Selection --- Trigger Python to open native dialog
        function triggerPythonFileDialog() {
            updateStatus('等待用户选择文件...', true);
            startButton.disabled = true;
            startButton.textContent = '请先选择文件';
            fileInfo.textContent = ''; // Clear previous selection display
            pythonProvidedFilePath = null; // Reset path

            // Call Python API to open the native file dialog
            window.pywebview.api.trigger_file_dialog()
                .catch(error => {
                    console.error('Error calling Python trigger_file_dialog:', error);
                    updateStatus(`请求文件选择出错: ${error}`, false, true);
                });
        }

        // --- Event Listeners ---
        // Make both the area and the button trigger the process
        fileSelectArea.addEventListener('click', triggerPythonFileDialog);
        browseButton.addEventListener('click', triggerPythonFileDialog);

        // --- Button Click ---
        startButton.addEventListener('click', () => {
            if (!pythonProvidedFilePath) {
                 updateStatus('错误：文件路径未确认，请重新选择文件。', false, true);
                 return;
            }

            const minutes = parseInt(minutesInput.value, 10);
            const seconds = parseInt(secondsInput.value, 10);

            if (isNaN(minutes) || isNaN(seconds) || (minutes === 0 && seconds === 0)) {
                updateStatus('错误：请输入有效的截图间隔（至少大于0秒）。', false, true);
                return;
            }

            const intervalSeconds = minutes * 60 + seconds;
            updateStatus('正在准备处理...', true);
            startButton.disabled = true;
            startButton.textContent = '处理中...';

            // Call Python start_processing (it already knows the path)
            window.pywebview.api.start_processing(intervalSeconds)
                .catch(error => {
                    console.error('Error calling Python API start_processing:', error);
                    updateStatus(`调用Python处理时出错: ${error}`, false, true);
                    startButton.disabled = false; // Re-enable on error
                    startButton.textContent = '开始截图';
                 });
        });

        // --- Communication with Python ---

        // Function for Python to call to update the status display
        function updateStatus(message, isProcessing = false, isError = false) {
            console.log("Updating status:", message);
            statusDiv.textContent = message;
            
            // Apply visual styling based on state
            statusDiv.className = isProcessing ? 'processing' : '';
            
            if (isError) {
                statusDiv.style.color = 'var(--error)';
            } else {
                statusDiv.style.color = 'var(--text-secondary)';
            }
        }

        // Function for Python to call when processing is complete
        function processingComplete(success, message) {
             updateStatus(message, false, !success);
             startButton.disabled = false; // Re-enable button
             startButton.textContent = '开始截图';
        }

         // Function for Python to call **after** native dialog confirms the path
         function setFilePath(path) {
             pythonProvidedFilePath = path; // Store the confirmed path
             const filename = path.split(/[\\/]/).pop(); // Extract filename for display
             fileInfo.textContent = `已选择: ${filename}`;
             startButton.disabled = false; // Enable start button
             startButton.textContent = '开始截图';
             updateStatus(`准备处理文件: ${filename}`);
         }

         // Add drag and drop support
         fileSelectArea.addEventListener('dragover', (e) => {
             e.preventDefault();
             fileSelectArea.style.backgroundColor = 'rgba(124, 77, 255, 0.2)';
         });
         
         fileSelectArea.addEventListener('dragleave', () => {
             fileSelectArea.style.backgroundColor = '';
         });
         
         fileSelectArea.addEventListener('drop', (e) => {
             e.preventDefault();
             fileSelectArea.style.backgroundColor = '';
             // Note: We don't actually handle the file here since we're using Python's native dialog
             // This just triggers the Python dialog as if the user had clicked
             triggerPythonFileDialog();
         });

         // Expose functions to Python
         window.updateStatus = updateStatus;
         window.processingComplete = processingComplete;
         window.setFilePath = setFilePath; // Python will call this after its dialog

         // Initial state
         updateStatus('请点击按钮选择视频文件。');
    </script>
</body>
</html>
